Component({
  properties: {
    fileVisible: { type: Boolean, value: false, },   // 订单附件
    fileList: { type: Array, value: [], },
  },
  data: {
    imageVisible: false,  // 图片预览
    imageList: [],
    downloadIcon: 'download'
  },
  methods: {
    ClosefileVisible() {
      this.triggerEvent('ClosefileVisible', { fileVisible: false });
    },
    /* 文件预览 */
    viewerFile(e) {
      const { type, url } = e.currentTarget.dataset.file;
      if (type != 'image') this.downLoadPDF(url)
      else this.setData({ imageList: [url], imageVisible: true })
    },
    /* 下载PDF文件 */
    downLoadPDF(url) {
      wx.downloadFile({
        url,
        success: function (resinfo) {
          let path = resinfo.tempFilePath;
          console.log(path, resinfo)
          wx.openDocument({
            filePath: path,
            fileType: 'pdf',
            showMenu: true,
            success: function (rest) {
              console.log('打开文件成功')
              console.log(rest);
            },
            fail: function (error) {
              wx.showToast({
                icon: 'none',
                title: '打开文件失败'
              });
            },
          })
        },
        fail: function (err) {
          console.log('fail')
          console.log(err)
          wx.showToast({
            icon: 'none',
            title: '下载文件失败'
          });
        }
      })
    },
    /* 下载图片 */
    downloadImg() {
      this.setData({ downloadIcon: 'loading' })
      let that = this;
      wx.downloadFile({
        url: that.data.imageList[0],//图片的地址
        type: 'audio',
        success: function (res) {
          const tempFilePath = res.tempFilePath  //如果请求成功，则通过res中的tempFilePath 得到需要下载的图片地址
          console.log(tempFilePath); //方便查看，这里打印路径，并且提示请求成功
          console.log("请求到了");
          wx.saveImageToPhotosAlbum({
            filePath: tempFilePath,  //设置下载图片的地址
            success: function () {
              wx.showToast({
                title: '保存成功',
                icon: 'none'
              })
              that.setData({ downloadIcon: 'check' })
              setTimeout(() => {
                that.setData({ downloadIcon: 'download' })
              }, 1000);
            }
          })
        }
      })
    },
    closeImageVisible() {
      this.setData({ imageList: [], imageVisible: false })
    },
  },
});
